package com.miwzn.boot.modules.sys.web;

import com.miwzn.base.AbstractController;
import com.miwzn.boot.modules.sys.entity.dto.SysDictDto;
import com.miwzn.boot.modules.sys.entity.po.SysDict;
import com.miwzn.boot.modules.sys.entity.po.SysMenu;
import com.miwzn.boot.modules.sys.entity.qm.SysDictQuery;
import com.miwzn.boot.modules.sys.entity.qm.SysMenuQuery;
import com.miwzn.boot.modules.sys.entity.dto.SysMenuDto;
import com.miwzn.boot.modules.sys.service.SysMenuService;
import com.miwzn.comm.ResponseEntity;
import com.miwzn.comm.annotation.OperationLog;
import com.miwzn.comm.constant.MyConstant;
import com.miwzn.comm.exception.BsRException;
import org.apache.commons.collections.CollectionUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresUser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: wzn
 * @Date: 2018-12-24 15:57:27
 * @Description: www.miwzn.com mvc api
 */
@ResponseBody
@Controller
@RequestMapping("sys/menu")
public class SysMenuController extends AbstractController<SysMenu, SysMenuDto, SysMenuQuery> {
    private static Logger logger = LoggerFactory.getLogger(SysMenuController.class);

    @Autowired
    private SysMenuService sysMenuService;

    /**
     * 根据ID获取信息
     *
     * @param id
     *
     * @return
     */
    @OperationLog(operation = "系统-菜单",content ="根据ID获取菜单")
    @RequiresPermissions("sys:menu:getById")
    @GetMapping("/getById/{id}")
    public ResponseEntity getById(@PathVariable("id") String id) {
        return super.getById(id);
    }

    /**
     * 列表
     *
     * @param entity
     *
     * @return
     */
    @OperationLog(operation = "系统-菜单",content ="获取菜单列表")
    @RequiresPermissions("sys:menu:list")
    @GetMapping("/list")
    public ResponseEntity list(SysMenuQuery entity) {
        return super.listD(entity);
    }

    /**
     * 添加
     *
     * @param entity
     *
     * @return
     */
    @OperationLog(operation = "系统-菜单",content ="添加菜单")
    @RequiresPermissions("sys:menu:add")
    @PostMapping("add")
    public ResponseEntity add(@RequestBody SysMenuDto entity) {
        checkMenu(entity);
        return super.add(entity);
    }

    /**
     * 更新
     *
     * @param entity
     *
     * @return
     */
    @OperationLog(operation = "系统-菜单",content ="更新菜单")
    @RequiresPermissions("sys:menu:update")
    @PutMapping("update")
    public ResponseEntity update(@RequestBody SysMenuDto entity) {
        checkMenu(entity);
        updateParentIds(entity);
        return super.update(entity);
    }

    private void checkMenu(SysMenuDto entity) {
        if (entity.getParentId() == null) {
            entity.setParentId("0");
            entity.setParentIds("0");
        }
        SysMenuQuery q = new SysMenuQuery();
        q.setDelFlag(MyConstant.FLAG_N);
        q.setName(entity.getName());
        q.setParentId(entity.getParentId());
        List<SysMenu> sysMenus = sysMenuService.select(q);
        //同一父级下不能有相同的CODE
        if (CollectionUtils.isNotEmpty(sysMenus)) {
            if (entity.getId() == null) throw new BsRException("同一组菜单名称不能重复");
            for (SysMenu sysMenu : sysMenus) {
                if (!sysMenu.getId().equalsIgnoreCase(entity.getId())) {
                    throw new BsRException("同一组菜单名称不能重复");
                }
            }
        }
    }


    /**
     * 递归处理下级数据
     *
     * @param entity
     */
    private void updateParentIds(SysMenuDto entity) {
        SysMenu p = sysMenuService.selectByPrimaryKey(entity.getId());
        if (!p.getParentId().equalsIgnoreCase(entity.getParentId())) {
            SysMenu query = new SysMenu();
            query.setParentId(entity.getId());
            List<SysMenu> pos = sysMenuService.select(query);
            if (CollectionUtils.isNotEmpty(pos)) {
                for (SysMenu po : pos) {
                    po.setParentId(entity.getId());
                    po.setParentIds(entity.getParentIds() + "," + entity.getId());
                    sysMenuService.updateByPrimaryKeySelective(po);
                    SysMenuDto dto = sysMenuService.wrapD(po);
                    updateParentIds(dto);
                }
            }
        }
    }

    /**
     * 软删除
     *
     * @param id
     *
     * @return
     */
    @OperationLog(operation = "系统-菜单",content ="删除菜单")
    @RequiresPermissions("sys:menu:delFlag")
    @DeleteMapping("/delFlag/{id}")
    public ResponseEntity deleteFlag(@PathVariable("id") String id) {
        return super.deleteFlag(id);
    }

    /**
     * 软删除
     *
     * @param ids
     *
     * @return
     */
    @OperationLog(operation = "系统-菜单",content ="删除菜单")
    @RequiresPermissions("sys:menu:delFlag")
    @DeleteMapping("/delFlag")
    public ResponseEntity deleteFlag(@RequestBody String[] ids) {
        return super.deleteFlag(ids);
    }

    /**
     * 硬删除
     *
     * @param id
     *
     * @return
     */
    @OperationLog(operation = "系统-菜单",content ="删除菜单")
    @RequiresPermissions("sys:menu:del")
    @DeleteMapping("/del/{id}")
    public ResponseEntity delete(@PathVariable("id") String id) {
        return super.delete(id);
    }

    /**
     * 硬删除
     *
     * @param ids
     *
     * @return
     */
    @OperationLog(operation = "系统-菜单",content ="删除菜单")
    @RequiresPermissions("sys:menu:del")
    @DeleteMapping("/del")
    public ResponseEntity delete(@RequestBody String[] ids) {
        return super.delete(ids);
    }

}
